package com.olclass.web.action;

import com.olclass.web.domain.College;
import com.olclass.web.domain.Lesson;
import com.olclass.web.domain.Lessonitem;
import com.olclass.web.domain.Lessonmedia;
import com.olclass.web.service.LessonService;
import com.olclass.web.util.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

/**
 * FileName: CollegeAction.java
 * Author:   Administrator
 * Date:     2021/3/23 19:18
 * Description:
 */
@RestController
@RequestMapping("/lesson")
public class LessonAction {

    private Logger logger = Logger.getLogger(LessonAction.class);

    @Autowired
    LessonService service;

    @RequestMapping(value = "/list.json", method = {RequestMethod.GET, RequestMethod.POST}, produces = {
            "application/json;charset=UTF-8"})
    @ResponseBody
    public BaseResponse queryByFilter(HttpServletRequest request, String filterParam, String sortParam, String page,
                                      String rows) {
        EasyUIResponse response = new EasyUIResponse();

        try {

            // 处理参数异常信息
            if (StringUtils.isBlank(page))
                throw new IllegalArgumentException("参数page不得为空");
            if (StringUtils.isBlank(rows))
                throw new IllegalArgumentException("参数rows不得为空");

            int start = Integer.valueOf(page) - 1;
            int limit = Integer.valueOf(rows);

            if (StringUtils.isBlank(filterParam)) {
                filterParam = "";
            }

            if (StringUtils.isBlank(sortParam)) {
                sortParam = "SORT_id_DESC";
            }

            Page<Lesson> apks = service.queryByFilter(filterParam, sortParam, start, limit);
            response.setTotal(apks.getTotalElements());
            response.setRows(apks.getContent());

        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            response.setStatus(CommonConstants.RESPONSE_STATUS_FAILURE);
            response.setMessage(ex.getMessage());
        }

        return response;
    }

    @RequestMapping(value = "/findById.json", method = { RequestMethod.GET, RequestMethod.POST }, produces = {
            "application/json;charset=UTF-8" })
    @ResponseBody
    public BaseResponse findById(HttpServletRequest request, String id) {
        BaseObjectResponse response = new BaseObjectResponse();

        try {

            Lesson item = service.findById(id);
            response.setData(item);

        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            response.setStatus(CommonConstants.RESPONSE_STATUS_FAILURE);
            response.setMessage(ex.getMessage());
        }
        return response;
    }

    @RequestMapping(value = "/save.json", method = {RequestMethod.GET, RequestMethod.POST}, produces = {
            "application/json;charset=UTF-8"})
    @ResponseBody
    public BaseResponse save(HttpServletRequest request, @RequestBody Lesson item) {
        BaseResponse response = new BaseResponse();
        try {
            String userid = SessionUtil.getUserId(request);
            item.setUserid(userid);
            service.save(item);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            response.setStatus(CommonConstants.RESPONSE_STATUS_FAILURE);
            response.setMessage(ex.getMessage());
        }
        return response;
    }

    @RequestMapping(value = "/delete.json", method = {RequestMethod.GET, RequestMethod.POST}, produces = {
            "application/json;charset=UTF-8"})
    @ResponseBody
    public BaseResponse delete(HttpServletRequest request, String id) {
        BaseResponse response = new BaseResponse();
        try {
            service.delete(id.toString());
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            response.setStatus(CommonConstants.RESPONSE_STATUS_FAILURE);
            response.setMessage(ex.getMessage());
        }
        return response;
    }

    @RequestMapping(value = "/saveLessonItem.json", method = {RequestMethod.GET, RequestMethod.POST}, produces = {
            "application/json;charset=UTF-8"})
    @ResponseBody
    public BaseResponse save(HttpServletRequest request, @RequestBody Lessonitem item) {
        BaseObjectResponse response = new BaseObjectResponse();
        try {
            item.setDate(new Date());
            service.saveLessonItem(item);
            Lesson lesson = service.findById(item.getPid());
            response.setData(lesson);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            response.setStatus(CommonConstants.RESPONSE_STATUS_FAILURE);
            response.setMessage(ex.getMessage());
        }
        return response;
    }

    @RequestMapping(value = "/deleteLessonItem.json", method = {RequestMethod.GET, RequestMethod.POST}, produces = {
            "application/json;charset=UTF-8"})
    @ResponseBody
    public BaseResponse deleteLessonItem(HttpServletRequest request, String id) {
        BaseObjectResponse response = new BaseObjectResponse();
        try {
            Lessonitem oneLessonItem = service.findOneLessonItem(id);
            String pid = oneLessonItem.getPid();
            service.deleteLessonItem(id);
            Lesson lesson = service.findById(pid);
            response.setData(lesson);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            response.setStatus(CommonConstants.RESPONSE_STATUS_FAILURE);
            response.setMessage(ex.getMessage());
        }
        return response;
    }

    @RequestMapping(value = "/deleteLessonMedia.json", method = {RequestMethod.GET, RequestMethod.POST}, produces = {
            "application/json;charset=UTF-8"})
    @ResponseBody
    public BaseResponse deleteLessonMedia(HttpServletRequest request, String id) {
        BaseObjectResponse response = new BaseObjectResponse();
        try {
            Lessonmedia oneLessonMedia = service.findOneLessonMedia(id);
            String pid = oneLessonMedia.getLessonid();
            service.deleteLessonMeida(id);
            Lessonitem oneLessonItem = service.findOneLessonItem(pid);
            response.setData(oneLessonItem);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            response.setStatus(CommonConstants.RESPONSE_STATUS_FAILURE);
            response.setMessage(ex.getMessage());
        }
        return response;
    }


    @RequestMapping(value = "/importFile.action", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public BaseResponse importTaskData(HttpServletRequest request, String lessonitem) {
        BaseObjectResponse response = new BaseObjectResponse();

        try {
            String userId = SessionUtil.getUserId(request);
            service.importTaskFile(request, lessonitem, userId);
            Lessonitem oneLessonItem = service.findOneLessonItem(lessonitem);
            response.setData(oneLessonItem);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            response.setStatus(CommonConstants.RESPONSE_STATUS_FAILURE);
            response.setMessage(e.getMessage());
        }
        return response;
    }
}